package org.jupiter.blog.web.controller;

import org.jupiter.blog.api.dto.JupiterArticleDTO;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articles")
public class JupiterArticleController {

	@Autowired
	private JupiterArticleService articleService;

	@GetMapping("/{uuid}")
	public JupiterResult<JupiterArticleDTO> getById(@PathVariable String uuid) {
		JupiterArticleDTO article = articleService.getById(uuid);
		return JupiterResult.success(article);
	}

	@GetMapping
	public JupiterResult<List<JupiterArticleDTO>> getAll() {
		List<JupiterArticleDTO> articles = articleService.getAll();
		return JupiterResult.success(articles);
	}

	@GetMapping("/page")
	public JupiterResult<Object> getByPage(
			@RequestParam(defaultValue = "1") int page,
			@RequestParam(defaultValue = "10") int size,
			@RequestParam(required = false) String title,
			@RequestParam(required = false) String categoryId,
			@RequestParam(required = false) Integer status) {

		//		List<JupiterArticleDTO> articles = articleService.getByPage(page, size, title, categoryId, status);
		//		long total = articleService.getCount(title, categoryId, status);

		return JupiterResult.success(null);
	}

	@PostMapping
	public JupiterResult<Void> create(@RequestBody JupiterArticleDTO article) {
		articleService.create(article);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}")
	public JupiterResult<Void> update(@PathVariable String uuid, @RequestBody JupiterArticleDTO article) {
		article.setUuid(uuid);
		articleService.update(article);
		return JupiterResult.success();
	}

	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> delete(@PathVariable String uuid) {
		articleService.delete(uuid);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}/status")
	public JupiterResult<Void> updateStatus(@PathVariable String uuid, @RequestParam Integer status) {
		articleService.updateStatus(uuid, status);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}/stick")
	public JupiterResult<Void> updateStick(@PathVariable String uuid, @RequestParam Integer isStick) {
		articleService.updateStick(uuid, isStick);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}/recommend")
	public JupiterResult<Void> updateRecommend(@PathVariable String uuid, @RequestParam Integer isRecommend) {
		articleService.updateRecommend(uuid, isRecommend);
		return JupiterResult.success();
	}

	@PutMapping("/{uuid}/carousel")
	public JupiterResult<Void> updateCarousel(@PathVariable String uuid, @RequestParam Integer isCarousel) {
		articleService.updateCarousel(uuid, isCarousel);
		return JupiterResult.success();
	}

	@GetMapping("/published")
	public JupiterResult<List<JupiterArticleDTO>> getPublishedArticles() {
		List<JupiterArticleDTO> articles = articleService.getPublishedArticles();
		return JupiterResult.success(articles);
	}

	@GetMapping("/stick")
	public JupiterResult<List<JupiterArticleDTO>> getStickArticles() {
		List<JupiterArticleDTO> articles = articleService.getStickArticles();
		return JupiterResult.success(articles);
	}

	@GetMapping("/recommend")
	public JupiterResult<List<JupiterArticleDTO>> getRecommendArticles() {
		List<JupiterArticleDTO> articles = articleService.getRecommendArticles();
		return JupiterResult.success(articles);
	}

	@GetMapping("/carousel")
	public JupiterResult<List<JupiterArticleDTO>> getCarouselArticles() {
		List<JupiterArticleDTO> articles = articleService.getCarouselArticles();
		return JupiterResult.success(articles);
	}
}
